﻿using DbEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Commen;

namespace DAL
{
    public class NewsService : BaseService
    {
        /// <summary>
        /// 插入新闻内容
        /// </summary>
        /// <param name="news"></param>
        /// <returns></returns>
        public int InsertNews(News news)
        {
            string sqlStr = "INSERT INTO news(ID,Title,Content,CreatDate) VALUES(@ID,@Title,@Content,@CreatDate)";
            return Db.ExecuteBySql(new StringBuilder(sqlStr), news);
        }
        public static int InsertNew(News news)
        {
            return new NewsService().InsertNews(news);
        }

       /// <summary>
       /// 事物插入数据
       /// </summary>
       /// <param name="news"></param>
       /// <returns></returns>
        public int PilianChaRus(News news)
        {
            try
            {
                string sqlStr1 = "INSERT INTO news(ID,Title,Content,CreatDate) VALUES(@ID,@Title,@Content,@CreatDate)";

                var obj1 = news;
                var sqlStr2 = "UPDATE spidercontent SET AfuterTitle=@AfuterTitle,HasTonbu=1 WHERE ID=@ID";
                var obj2 = new { ID = news.ID, AfuterTitle = news.Title };
                object[] sql = new object[2]; sql[0] =new StringBuilder( sqlStr1); sql[1] =new StringBuilder( sqlStr2);
                object[] par = new object[2]; par[0] = obj1; par[1] = obj2;
                return Db.BatchExecuteBySql(sql, par);
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        public static int PilianChaRu(News news)
        {
            return new NewsService().PilianChaRus(news);
        }

        /// <summary>
        /// 伪原创替换
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public string WeiYuanChuang(string content)
        {
            if (content.IsNullOrWhiteSpace())
                return string.Empty;
            worldsService ctx = new worldsService();
            var listword = ctx.GetRuandList();

            foreach (var word in listword)
            {
                if (content.Contains(word.oldw) == true)
                    content = content.Replace(word.oldw, word.neww);
            }
            return content;
        }
    }
}
